package com.qdsgjsfk.vision.model;

import org.json.JSONException;
import org.json.JSONObject;

public class Msg {

    public int code;
    public int size;
    public int mode;
    public int chartType;
    public String value;

    public Msg() {
    }

    public Msg(Builder builder) {
        this.code = builder.code;
        this.size = builder.size;
        this.chartType = builder.chartType;
        this.value = builder.value;
        this.mode = builder.mode;
    }

    public static final class Builder {
        private int code;
        private int size = -1;
        private int chartType = -1;
        private int mode = -1;
        private String value;

        public Builder mode(int val) {
            mode = val;
            return this;
        }

        public Builder code(int val) {
            code = val;
            return this;
        }

        public Builder size(int val) {
            size = val;
            return this;
        }

        public Builder chartType(int val) {
            chartType = val;
            return this;
        }

        public Builder value(String val) {
            value = val;
            return this;
        }

        public Builder() {
        }

        public Msg build() {
            return new Msg(this);
        }
    }

    public Msg(int code, int size, String value, int chartType, int mode) {
        this.code = code;
        this.size = size;
        this.mode = mode;
        this.value = value;
        this.chartType = chartType;
    }

    public Msg(String msg) {
        try {
            JSONObject object = new JSONObject(msg);
            code = object.optInt("code");
            size = object.optInt("size");
            value = object.optString("value");
            mode = object.optInt("mode");
            chartType = object.optInt("chartType");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        JSONObject object = new JSONObject();
        try {
            object.put("code", code);
            object.put("size", size);
            object.put("value", value);
            object.put("mode", mode);
            object.put("chartType", chartType);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }
}